﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common; 
using System.Xml.Linq;


namespace DbSharp.Tools.DbConnectionBuilder.Core
{
    static class ConnectionSectionBuilder
    {
        public static string BuildConnectionSection(string providerName, string connectionString)
        {
            string format = @"<dbConnection connectionName="" "" 
        connectionString=""{0}""
        providerName=""{1}"" />";
            connectionString = connectionString.Replace(@"""", "'");
            string connectionSection = string.Format(format, connectionString, providerName);
            return connectionSection;
        }

        public static bool TryParseConnectionSection(string connectionSection, out string connectionString, out string providerName)
        {
            /* <dbConnection connectionName="db1"
      connectionString="Data Source=VM_192;Persist Security Info=True;User ID=HR;Password=HR;Unicode=True"
      providerName="System.Data.OracleClient" />*/

            bool result = false;
            try
            {
                XElement sectionElement = XElement.Parse(connectionSection);
                connectionString = sectionElement.Attribute("connectionString").Value;
                providerName = sectionElement.Attribute("providerName").Value;
                result = true;
            }
            catch 
            {
                connectionString = string.Empty;
                providerName = string.Empty;
                result = false;
            }
            return result;
        }
        
        public static string GetProviderName(string connectionSection)
        {
            /* <dbConnection connectionName="db1"
       connectionString="Data Source=VM_192;Persist Security Info=True;User ID=HR;Password=HR;Unicode=True"
       providerName="System.Data.OracleClient" />*/
  
            string flagString = "providerName=\"";
            string result = string.Empty;
            var index = connectionSection.IndexOf(flagString, StringComparison.OrdinalIgnoreCase);
            int startIndex = index + flagString.Length;

            if (connectionSection.Length <= (startIndex + 1))
                return result;

            var endIndex = connectionSection.IndexOf("\"", startIndex, StringComparison.Ordinal);
            if (index >= 0)
            {
                if (endIndex < 0)
                    result = connectionSection.Substring(startIndex, connectionSection.Length);
                else
                    result = connectionSection.Substring(startIndex, endIndex - startIndex);
            }
            return result.Trim();
        }
         
    }



    static class ConnectionStringHelper
    {
        /// <summary>
        /// Get value for key
        /// </summary>
        /// <param name="keyString">e.g.Data Source=, or User ID=</param>
        /// <param name="connectionstring"></param>
        /// <returns>get value of specific key</returns>
        public static string ExtractKeyValue(string keyString, string connectionstring)
        {
            /* Oracle connection string sample
             * <dbConnection connectionName="db1"
        connectionString="Data Source=VM_192;Persist Security Info=True;User ID=HR;Password=HR;Unicode=True"
        providerName="System.Data.OracleClient" />*/

            /*SQL server connection string sample
             * Data Source=SHNI317\SQLEXPRESS;Initial Catalog=model;Integrated Security=True*/

            string result = string.Empty;
            var index = connectionstring.IndexOf(keyString, StringComparison.OrdinalIgnoreCase);
            int startIndex = index + keyString.Length;

            if (connectionstring.Length <= (startIndex + 1))
                return result;

            var endIndex = connectionstring.IndexOf(";", startIndex, StringComparison.Ordinal);
            if (endIndex < 0)
                endIndex = connectionstring.IndexOf("\"", startIndex, StringComparison.Ordinal);
            if (index >= 0)
            {
                if (endIndex < 0)
                    result = connectionstring.Substring(startIndex, connectionstring.Length);
                else
                    result = connectionstring.Substring(startIndex, endIndex - startIndex);
            }
            return result;
        }
    }
}
